#!/usr/bin/perl

# $Id: 18_dbwrapper_databasebuilder.t 274 2010-07-18 13:35:09Z dolbanavt $
# $Date: 2010-07-18 16:35:30 +0300 (Вск, 18 Июл 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/reborn/t/18_dbwrapper_databasebuilder.t $
# $LastChangedRevision$

use FindBin qw( $RealBin );
use File::Spec::Functions qw( catfile );

BEGIN {
    push @INC, catfile( $RealBin, '..', 'base_lib' );
    push @INC, catfile( $RealBin, '..', 'lib' );
}

use JIP::ToolSet;
use English qw( -no_match_vars );
use Test::More 0.88;
use Const::Fast qw( const );
use Test::Exception;

use DBI;

const my $MODULE => 'DBWrapper::DatabaseBuilder';

use_ok $MODULE;
require_ok $MODULE;

can_ok $MODULE, qw(
    new _init
    set_dbh get_dbh
    get_hosts_for_load
    remove_files_older_then
    remove_files_for
    update_status_for
    add_new_file
    add_new_dir
);

{
    my $w = init_wrapper();

    ok $w, 'got object';

    isa_ok $w, $MODULE;
    isa_ok $w, 'DBWrapper';
    isa_ok $w, 'JIP::BaseClass';
    isa_ok $w, 'Class::Accessor::Fast';

    is ref $w->get_dbh, 'DBI::db', 'dbh accessor';

    throws_ok { $w->remove_files_older_then(); }
        qr{^ageing\srequired}xms,
        'remove_files_older_then( ageing )';

    throws_ok { $w->get_hosts_for_load(); }
        qr{^ageing\srequired}xms,
        'get_hosts_for_load( ageing )';

    throws_ok { $w->remove_files_for(); }
        qr{^host_id\srequired}xms,
        'remove_files_for( host_id )';

    throws_ok { $w->update_status_for(); }
        qr{^host_id\srequired}xms,
        'update_status_for( host_id )';

    throws_ok { $w->add_new_file(); }
        qr{^item\srequired}xms,
        'add_new_file( item )';

    throws_ok { $w->add_new_dir(); }
        qr{^item\srequired}xms,
        'add_new_dir( item )';
}

#-- remove_files_older_then() --------------------------------------------------
{
    my $w = init_wrapper();

    my $result = $w->remove_files_older_then( 42 );

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       2,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       1,
       'correct number of parameters bound for statement 1';

    is scalar @{ $dbh_history->[1]->bound_params },
       1,
       'correct number of parameters bound for statement 2';

    is $dbh_history->[0]->bound_params->[0],
       42,
       'correct value for parameter 1';

    is $dbh_history->[1]->bound_params->[0],
       42,
       'correct value for parameter 2';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->remove_files_older_then( 42 );
}
qr{cannot\supdate\sdata:\sNo\sconnection\spresent}xms,
'"cannot_update_data" exception';

#-- get_hosts_for_load() -------------------------------------------------------
{
    my $w = init_wrapper();

    $w->get_dbh->{'mock_add_resultset'} = [
        [ 'host_identifier', 'host_ip_address' ],
        [ 42, '10.0.14.140' ],
        [ 43, '192.168.1.1'],
    ];

    # resultsets testing
    my $ageing = 42;

    my @hosts = $w->get_hosts_for_load( $ageing );

    is scalar @hosts, 2, 'correct number of resultsets';

    is ref $hosts[0], 'Host', 'returned Host object in first resultset';
    is ref $hosts[1], 'Host', 'returned Host object in second resultset';

    is $hosts[0]->get_id, 42, 'returned host_identifier in first resultset';
    is $hosts[1]->get_id, 43, 'returned host_identifier in second resultset';

    is $hosts[0]->get_ip_address,
       '10.0.14.140',
       'returned host_ip_address in first resultset';

    is $hosts[1]->get_ip_address,
       '192.168.1.1',
       'returned host_ip_address in first resultset';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       1,
       'correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       $ageing,
       'correct value for parameter 1';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->get_hosts_for_load( 42 );
}
qr{cannot\sprepare:\sNo\sconnection\spresent}xms,
'"cannot_prepare" exception';

#-- update_status_for() --------------------------------------------------------
{
    my $w = init_wrapper();

    my $host_id = 42;

    my $result = $w->update_status_for( $host_id );

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       1,
       'correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       $host_id,
       'correct value for parameter 1';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->update_status_for( 42 );
}
qr{cannot\sinsert\sdata:\sNo\sconnection\spresent}xms,
'"cannot_insert_data" exception';

#-- remove_files_for() ---------------------------------------------------------
{
    my $w = init_wrapper();

    my $host_id = 42;

    my $result = $w->remove_files_for( $host_id );

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       1,
       'correct number of parameters bound for statement 1';

    is $dbh_history->[0]->bound_params->[0],
       $host_id,
       'correct value for parameter 1';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->remove_files_for( 42 );
}
qr{cannot\supdate\sdata:\sNo\sconnection\spresent}xms,
'"cannot_update_data" exception';

#-- add_new_file() -------------------------------------------------------------
{
    my $w = init_wrapper();

    my $result = $w->add_new_file( {
        host_id => 'tratata_1',
        type    => 'tratata_2',
        name    => 'tratata_3',
        path    => 'tratata_4',
        size    => 'tratata_5',
        content => 'tratata_6',
    } );

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       5,
       'correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       'tratata_1',
       'correct value for parameter 1';

    is $dbh_history->[0]->bound_params->[1],
       'tratata_3',
       'correct value for parameter 2';

    is $dbh_history->[0]->bound_params->[2],
       'tratata_4',
       'correct value for parameter 3';

    is $dbh_history->[0]->bound_params->[3],
       'tratata_5',
       'correct value for parameter 4';

    is $dbh_history->[0]->bound_params->[4],
       'tratata_6',
       'correct value for parameter 5';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->add_new_file( { item => q{} } );
}
qr{cannot\sinsert\sdata:\sNo\sconnection\spresent}xms,
'"cannot_insert_data" exception';

#-- add_new_dir() --------------------------------------------------------------
{
    my $w = init_wrapper();

    my $result = $w->add_new_dir( {
        host_id => 'tratata_1',
        type    => 'tratata_2',
        name    => 'tratata_3',
        path    => 'tratata_4',
    } );

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       3,
       'correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       'tratata_1',
       'correct value for parameter 1';

    is $dbh_history->[0]->bound_params->[1],
       'tratata_3',
       'correct value for parameter 2';

    is $dbh_history->[0]->bound_params->[2],
       'tratata_4',
       'correct value for parameter 3';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->add_new_dir( { item => q{} } );
}
qr{cannot\sinsert\sdata:\sNo\sconnection\spresent}xms,
'"cannot_insert_data" exception';

done_testing();

#-- subroutines ----------------------------------------------------------------
sub init_wrapper {
    my $dbh = DBI->connect( 'DBI:Mock:', q{}, q{} );

    $dbh->{'PrintError'} = 0;

    return $MODULE->new( dbh => $dbh );
}

#-- end ------------------------------------------------------------------------
